package gameClass;

import gameTypes.Vector2;
import globals.Global;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ben Elliott
 */
public class SpriteAnimation {
	private BufferedImage t2dTexture;
	private Map<String, FrameAnimation> faAnimations = new HashMap<String, FrameAnimation>();
	
	public boolean bAnimating = true;
	public Vector2 v2Position = new Vector2(0, 0);
	public Vector2 v2LastPosition = new Vector2(0, 0);
	public Vector2 v2Center;
    private String sCurrentAnimation = null;
    public int iWidth;
    public int iHeight;
    
    public SpriteAnimation(BufferedImage texture)
    {
        t2dTexture = texture;
    }
    
    public void AddAnimation(String Name, int X, int Y, int Width, int Height, int Frames, float FrameLength)
    {
        faAnimations.put(Name, new FrameAnimation(X, Y, Width, Height, Frames, FrameLength));
        iWidth = Width;
        iHeight = Height;
        v2Center = new Vector2(iWidth / 2, iHeight / 2);
    }

    public void AddAnimation(String Name, int X, int Y, int Width, int Height, int Frames,
       float FrameLength, String NextAnimation)
    {
        faAnimations.put(Name, new FrameAnimation(X, Y, Width, Height, Frames, FrameLength, NextAnimation));
        iWidth = Width;
        iHeight = Height;
        v2Center = new Vector2(iWidth / 2, iHeight / 2);
    }
    
    public FrameAnimation GetAnimationByName(String Name)
    {
        if (faAnimations.containsKey(Name))
        {
            return faAnimations.get(Name);
        }
        else
        {
            return null;
        }
    }

    public void setCurrentAnimation(String nextAnimation) {
    	if (faAnimations.containsKey(nextAnimation))
        {
            sCurrentAnimation = nextAnimation;
            faAnimations.get(sCurrentAnimation).CurrentFrame = 0;
            faAnimations.get(sCurrentAnimation).PlayCount = 0;
        }
    }
    
    public void Update(float elapsed)
    {
        // Don't do anything if the sprite is not animating
        if (bAnimating)
        {
            // If there is not a currently active animation
            if (sCurrentAnimation == null)
            {
                // Make sure we have an animation associated with this sprite
                if (faAnimations.size() > 0)
                {
                    // Set the active animation to the first animation
                    // associated with this sprite
                    String[] sKeys = new String[faAnimations.size()];
                    faAnimations.keySet().toArray(sKeys);
                    sCurrentAnimation = sKeys[0];
                }
                else
                {
                    return;
                }
            }

            // Run the Animation's update method
            faAnimations.get(sCurrentAnimation).Update(elapsed);

            // Check to see if there is a "followup" animation named for this animation
            if (faAnimations.get(sCurrentAnimation).NextAnimation != null)
            {
                // If there is, see if the currently playing animation has
                // completed a full animation loop
                if (faAnimations.get(sCurrentAnimation).PlayCount > 0)
                {
                    // If it has, set up the next animation
                	setCurrentAnimation(faAnimations.get(sCurrentAnimation).NextAnimation);
                }
            }
        }
    }
    
    public void Draw(Graphics graphics, int x, int y) {
    	if (bAnimating) {
    		if (sCurrentAnimation != null) {
		    	Rectangle r = faAnimations.get(sCurrentAnimation).getRectangle();
		    	graphics.drawImage(
						// The sprite
		    			t2dTexture.getSubimage(
		    					r.x,
		    					r.y,
		    					r.width,
		    					r.height),
						// Screen position rectangle
						x, 
						y,
						r.width,
						r.height,
						Global.Instance().imgObs);
    		}
    	}
    }
}

